package com.lego.dao.impl;

import com.lego.dao.UserDao;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.lego.bean.Reviews;
import com.lego.bean.User;
import com.lego.bean.UserReviewVO;
import com.lego.dao.BaseDao;
import com.lego.dao.UserDao;

public class UserDaoImpl implements UserDao {

	@Override
	public User selectUser(String userName, String password) {
		// TODO Auto-generated method stub
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet res = null;
		User user = null;
		String sql = "SELECT * FROM user WHERE userName = ? and password = ?";
		try {
			con = BaseDao.getCon();
			ps = con.prepareStatement(sql);
			ps.setString(1, userName);
			ps.setString(2, password);
			System.out.println(ps);
			res = ps.executeQuery();
			if(res.next()) {
				user = new User();
				user.setUserId(res.getInt("userId"));
				user.setUserName(res.getString("username"));
				user.setPassword(res.getString("password"));
				user.setPhoneNum(res.getString("PhoneNum"));
				user.setNickName(res.getString("nickName"));
				user.setHeadImg(res.getString("headImg"));
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			BaseDao.closeAll(con, ps, res);
		}
		
		return user;
	}
	@Override
	public int register(String userName, String password, String phoneNum) {
		// TODO Auto-generated method stub
		Connection con = null;
		PreparedStatement ps = null;
		int result = 0;
		String sql = "INSERT INTO user(userName,password,phoneNum,nickName,headImg) "
				+ "VALUES (?,?,?,?,?)";
		try {
			con = BaseDao.getCon();
			ps = con.prepareStatement(sql);
			ps.setString(1, userName);
			ps.setString(2, password);
			ps.setString(3, phoneNum);
			ps.setString(4,"乐购用户");
			ps.setString(5, "lego/headImg/default.jpg");
			System.out.println(ps);
			result = ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			BaseDao.closeAll(con, ps, null);
		}
		return result;
	}

	@Override
	public User selectUserByName(String userName) {
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet res = null;
		User user = null;
		String sql = "SELECT * FROM user WHERE userName = ?";
		try {
			con = BaseDao.getCon();
			ps = con.prepareStatement(sql);
			ps.setString(1, userName);
			
			res = ps.executeQuery();
			if(res.next()) {
				user = new User();
				user.setUserId(res.getInt("userId"));
				user.setUserName(res.getString("userName"));
				user.setPassword(res.getString("password"));
				user.setPhoneNum(res.getString("phoneNum"));
				user.setNickName(res.getString("NickName"));
				user.setHeadImg(res.getString("headImg"));
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			BaseDao.closeAll(con, ps, res);
		}
		
		return user;
	}
	@Override
	public List<UserReviewVO> selectUserReviewVO(int goodsId) {
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet res = null;
		List<UserReviewVO> list = new ArrayList<UserReviewVO>();
		String sql = "SELECT * FROM  USER u INNER JOIN reviews r ON u.userId = r.userId WHERE u.userId = ? GROUP BY u.userId, r.reviewId";
		try {
			con = BaseDao.getCon();
			ps = con.prepareStatement(sql);
			ps.setInt(1, goodsId);
			System.out.println(ps);
			res = ps.executeQuery();
			if(res.next()) {
				UserReviewVO urVo = new UserReviewVO();
				Reviews review = new Reviews();
				urVo.setUserId(res.getInt("userId"));
				urVo.setUserName(res.getString("username"));
				urVo.setPassword(res.getString("password"));
				urVo.setPhoneNum(res.getString("PhoneNum"));
				urVo.setNickName(res.getString("nickName"));
				urVo.setHeadImg(res.getString("headImg"));
				review.setReviewId(res.getInt("reviewId"));
			    review.setOrderId(res.getInt("orderId"));
			    review.setGoodsId(res.getInt("goodsId"));
			    review.setUserId(res.getInt("userId"));
			    review.setReviewTime(res.getDate("reviewTime"));
			    review.setReviewContext(res.getString("rewContext"));
			    urVo.setReview(review);
			    list.add(urVo);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			BaseDao.closeAll(con, ps, res);
		}
		return list;
	}
	@Override
	public int changeNickName(String nickName, int userId) {
		Connection con = null;
		PreparedStatement ps = null;
		int result = 0;
		String sql = "UPDATE user SET nickName = ? WHERE userId = ?";
		try {
			con = BaseDao.getCon();
			ps = con.prepareStatement(sql);
			ps.setString(1, nickName);
			ps.setInt(2, userId);
			System.out.println(ps);
			result = ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			BaseDao.closeAll(con, ps, null);
		}
		return result;
	}
	@Override
	public int changePwd(String password, int userId) {
		Connection con = null;
		PreparedStatement ps = null;
		int result = 0;
		String sql = "UPDATE user SET password = ? WHERE userId = ?";
		try {
			con = BaseDao.getCon();
			ps = con.prepareStatement(sql);
			ps.setString(1, password);
			ps.setInt(2, userId);
			System.out.println(ps);
			result = ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			BaseDao.closeAll(con, ps, null);
		}
		return result;
	}
	
	@Override
	public int changeHeadImg(String visitPath, int userId) {
		Connection con = null;
		PreparedStatement ps = null;
		int result = 0;
		String sql = "UPDATE user SET headImg = ? WHERE userId = ?";
		try {
			con = BaseDao.getCon();
			ps = con.prepareStatement(sql);
			ps.setString(1, visitPath);
			ps.setInt(2, userId);
			System.out.println(ps);
			result = ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			BaseDao.closeAll(con, ps, null);
		}
		return result;
	}
	@Override
	public int queryUsersRow(String userName) {
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet res = null;
		int totalRows = 0;
		String sql = "SELECT COUNT(*) AS totalRows FROM user";
		if(userName!=null&&!userName.equals("")) {
			sql+=" WHERE userName = ?";
		}
		try {
			con = BaseDao.getCon();
			ps = con.prepareStatement(sql);
			if(userName!=null&&!userName.equals("")) {
				ps.setString(1, userName);
			}
			System.out.println(ps);
			res = ps.executeQuery();
			if(res.next()) {
				totalRows = res.getInt("totalRows");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			BaseDao.closeAll(con, ps, res);
		}
		return totalRows;
	}
	@Override
	public List<User> queryAllUsers(String userName,int pageNum,int pageSize) {
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet res = null;
		List<User> list = new ArrayList<User>();
		List<Object> params = new ArrayList<Object>();
		String sql = "SELECT * FROM user ";
		System.out.println(userName);
		if(userName!=null&&!userName.equals("")) {
			sql+=" WHERE userName = ? ";
			params.add(userName);
		}
		sql+=" LIMIT ?,?";
		int newPageNum = (pageNum-1)*pageSize;
		params.add(newPageNum);
		params.add(pageSize);
		try {
			con = BaseDao.getCon();
			ps = con.prepareStatement(sql);
			for (int i = 0; i < params.size(); i++) {
				ps.setObject(i+1, params.get(i));
			}
			System.out.println(ps);
			res = ps.executeQuery();
			while(res.next()) {
				User user = new User();
				user.setUserId(res.getInt("userId"));
				user.setUserName(res.getString("username"));
				user.setPassword(res.getString("password"));
				user.setPhoneNum(res.getString("PhoneNum"));
				user.setNickName(res.getString("nickName"));
				user.setHeadImg(res.getString("headImg"));
				user.setIsDeleted(res.getInt("isDeleted"));
			    list.add(user);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			BaseDao.closeAll(con, ps, res);
		}
		return list;
	}
	@Override
	public int deleteUser(int userId) {
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet res = null;
		int rows = 0;
		String sql = "DELETE FROM user WHERE userId = ?";
		try {
			con = BaseDao.getCon();
			ps = con.prepareStatement(sql);
			ps.setInt(1, userId);
			System.out.println(ps);
			rows = ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			BaseDao.closeAll(con, ps, res);
		}
		return rows;
	}
	@Override
	public int changeUserStatus(int i, int userId) {
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet res = null;
		int rows = 0;
		String sql = "UPDATE user SET isDeleted = ? WHERE userId = ?";
		try {
			con = BaseDao.getCon();
			ps = con.prepareStatement(sql);
			ps.setInt(1, i);
			ps.setInt(2, userId);
			System.out.println(ps);
			rows = ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			BaseDao.closeAll(con, ps, res);
		}
		return rows;
	}
	@Override
	public int updateUser(User user) {
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet res = null;
		int rows = 0;
		String sql = "UPDATE user SET password=?,nickname=? WHERE userId = ?";
		try {
			con = BaseDao.getCon();
			ps = con.prepareStatement(sql);
			ps.setString(1, user.getPassword());
			ps.setString(2, user.getNickName());
			ps.setInt(3, user.getUserId());
			System.out.println(ps);
			rows = ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			BaseDao.closeAll(con, ps, res);
		}
		return rows;
	}
}
